package cn.lzd.mr.case2;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * Created by lzd on 2018/5/31.
 * 求取网站访问次数Top.N
 *
 * 方案一：就是如下这种解决。弊端所有数据都放在list里边，耗内存。
 *
 * 方案二：先进行第一遍mapreduce ,进行分类统计。
 * 第二遍基于第一次的结论，将每个维度放进对象，实现WritableComparable ，最后直接输出。会自动排序。
 *
 */
public class MRdemo {
    static String path = MRdemo.class.getResource("/case2/").getPath();

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        /*
         * 通过加载classpath下的*-site.xml文件解析参数
         */
        //Configuration conf = new Configuration();
        //conf.addResource("core-site.xml");

        /*
         * 通过代码设置参数
         */
        //conf.setInt("top.n",3);
        //conf.setInt("top.n",Integer.parseInt(args[0]));

        /*
         * 通过属性配置文件获取参数
         */
        //Properties props = new Properties();
        //props.load(JobSubmitter.class.getClassLoader().getResourceAsStream("topn.properties"));
        //conf.setInt("top.n", Integer.parseInt(pros.getProperty("top.n")))


        Configuration configuration = new Configuration();
        configuration.setInt("top.n",5);
        Job job = Job.getInstance(configuration);
        job.setJarByClass(MRdemo.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Bean.class);

        job.setMapperClass(Mdemo.class);
        job.setReducerClass(Rdemo.class);

        FileInputFormat.setInputPaths(job, new Path(path + "text"));
        FileOutputFormat.setOutputPath(job, new Path(path + "out/"));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
